//
//  ThreeView.m
//  YDD
//
//  Created by 欧洪飞 on 2017/8/22.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "ThreeView.h"
#import "SelectHouseTypeView.h"
#import "SelectRegionViewController.h"

@implementation ThreeView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)layoutSubviews{
    UITapGestureRecognizer *zhuangGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(zhuangEvent)];
    [self.zhuangText addGestureRecognizer:zhuangGest];
    
    UITapGestureRecognizer *caoGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(caoEvent)];
    [self.caoText addGestureRecognizer:caoGest];
    
    UITapGestureRecognizer *louGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(louEvent)];
    [self.louText addGestureRecognizer:louGest];
    
    UITapGestureRecognizer *yongGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(yongEvent)];
    [self.yongText addGestureRecognizer:yongGest];
    
    UITapGestureRecognizer *typeGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(typeEvent)];
    [self.typeText addGestureRecognizer:typeGest];
    
    UITapGestureRecognizer *jianGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jianEvent)];
    [self.yearsText addGestureRecognizer:jianGest];
    
    UITapGestureRecognizer *quGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(quEvent)];
    [self.quText addGestureRecognizer:quGest];
}

- (void)zhuangEvent{
    NSMutableArray *dataArray = [[NSMutableArray alloc]initWithObjects:@"毛坯",@"简装",@"中装",@"精装", nil];
    SelectHouseTypeView *pay = [[SelectHouseTypeView alloc]init];
    [pay showSelectTitle:@"装修" dataArray:dataArray count:1];
    pay.typePickSelect = ^(NSString *date) {
        [self.zhuangText setText:date];
    };
}

- (void)caoEvent{
    NSMutableArray *dataArray = [[NSMutableArray alloc]initWithObjects:@"东",@"南",@"西",@"北",@"东南",@"西南",@"东北",@"西北",@"东西",@"南北", nil];
    SelectHouseTypeView *pay = [[SelectHouseTypeView alloc]init];
    [pay showSelectTitle:@"朝向" dataArray:dataArray count:1];
    pay.typePickSelect = ^(NSString *date) {
        [self.caoText setText:date];
    };
}

- (void)louEvent{
    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    NSMutableArray *oneArr = [[NSMutableArray alloc]init];
    [oneArr addObject:@"-2层"];
    [oneArr addObject:@"-1层"];
    NSMutableArray *twoArr = [[NSMutableArray alloc]init];
    for (int i = 1; i< 100; i++) {
        [oneArr addObject:[NSString stringWithFormat:@"%zd层",i]];
        [twoArr addObject:[NSString stringWithFormat:@"%zd层",i]];
    }
    [dataArray addObject:oneArr];
    [dataArray addObject:twoArr];
    SelectHouseTypeView *pay = [[SelectHouseTypeView alloc]init];
    [pay showSelectTitle:@"楼层" dataArray:dataArray count:2];
    pay.typePickSelect = ^(NSString *date) {
        [self.louText setText:date];
    };
}

- (void)yongEvent{
    NSMutableArray *dataArray = [[NSMutableArray alloc]initWithObjects:@"住房",@"商房",@"办公楼",@"商铺", nil];
    SelectHouseTypeView *pay = [[SelectHouseTypeView alloc]init];
    [pay showSelectTitle:@"用途" dataArray:dataArray count:1];
    pay.typePickSelect = ^(NSString *date) {
        [self.yongText setText:date];
    };
}

- (void)typeEvent{
    NSMutableArray *dataArray = [[NSMutableArray alloc]initWithObjects:@"住房",@"商住",@"写字楼",@"别墅",@"商铺", nil];
    SelectHouseTypeView *pay = [[SelectHouseTypeView alloc]init];
    [pay showSelectTitle:@"类型" dataArray:dataArray count:1];
    pay.typePickSelect = ^(NSString *date) {
        [self.typeText setText:date];
    };
}

- (void)jianEvent{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate* dt = [NSDate date];
    NSDateComponents* comp = [gregorian components: NSCalendarUnitYear fromDate:dt];
    NSLog(@"现在是%ld年" , comp.year);
    NSInteger year = comp.year + 20;
    NSInteger allcount = year - 1941;
    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    for (int i = 0; i <  allcount + 1; i ++) {
        [dataArray addObject:[NSString stringWithFormat:@"%zd年",1941 + i]];
    }
    
    SelectHouseTypeView *pay = [[SelectHouseTypeView alloc]init];
    [pay showSelectTitle:@"建于" dataArray:dataArray count:1];
    pay.typePickSelect = ^(NSString *date) {
        [self.yearsText setText:date];
    };
}

- (void)quEvent{
    SelectRegionViewController *select = [[SelectRegionViewController alloc]init];
    [[self getCurrentVC].navigationController pushViewController:select animated:YES];
    select.xiaoquSelect = ^(BMKPoiInfo *model) {
        [self.quText setText:model.name];
        self.baiduInfo = model;
    };
}

- (UIViewController *)getCurrentVC {
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    //app默认windowLevel是UIWindowLevelNormal，如果不是，找到UIWindowLevelNormal的
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    id nextResponder = nil;
    UIViewController *appRootVC=window.rootViewController;
    // 如果是present上来的appRootVC.presentedViewController 不为nil
    if (appRootVC.presentedViewController) {
        nextResponder = appRootVC.presentedViewController;
    }else{
        NSLog(@"===%@",[window subviews]);
        UIView *frontView = [[window subviews] objectAtIndex:0];
        nextResponder = [frontView nextResponder];
    }
    if ([nextResponder isKindOfClass:[UITabBarController class]]){
        UITabBarController * tabbar = (UITabBarController *)nextResponder;
        UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
        // UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行
        result=nav.childViewControllers.lastObject;
    }else if ([nextResponder isKindOfClass:[UINavigationController class]]){
        UIViewController * nav = (UIViewController *)nextResponder;
        result = nav.childViewControllers.lastObject;
    }else{
        result = nextResponder;
    }
    return result;
}


@end
